#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include "SideWalk.h"
SideWalk* SideWalk_Create()
{
    SideWalk* sidewalk = (SideWalk*)malloc(sizeof(SideWalk));
    sidewalk->front = 0;
    sidewalk->rear = 0;
    sidewalk->max = N;
    sidewalk->sum = 0;
    return sidewalk;
}
void SideWalk_Free(SideWalk* sidewalk)
// 释放便道空间，以删除便道。
{
    free(sidewalk->sidewalkCar);
    free(sidewalk);
}

void SideWalk_MakeEmpty(SideWalk* sidewalk)
// 将便道置空。
{
    sidewalk->front = 0;
    sidewalk->rear = 0;
}

bool SideWalk_IsEmpty(SideWalk* sidewalk)
// 判断便道是否为空，为空返回true，否则返回false。
{
    if (sidewalk->front == sidewalk->rear)
        return true;
    else return false;
}

bool SideWalk_IsFull(SideWalk* sidewalk)
// 判断便道是否为满。为满返回true,否则返回false。
{
    if ((sidewalk->rear + 1) % sidewalk->max == sidewalk->front)
        return  true;
    else return false;
}

int SideWalk_Length(SideWalk* sidewalk)
// 便道长度。
{
    return (sidewalk->rear - sidewalk->front + sidewalk->max) % sidewalk->max;
}

bool SideWalk_Add(SideWalk* sidewalk, Car* car)//参数检查？？？
// 将car进入便道。若进入失败(便道满)，则返回false，否则返回true。
{
    if (!SideWalk_IsFull(sidewalk))
    {
        strcpy(sidewalk->sidewalkCar[sidewalk->rear].license_plate, car->license_plate);
        sidewalk->rear = (sidewalk->rear + 1) % sidewalk->max;
        printf("车辆已进入便道\n");
    }
    else {
        printf("便道已满\n");
        return false;
    }
}

bool SideWalk_Leave(SideWalk* sidewalk, Car& car)//参数检查？？？
// 从便道出队一个元素，返回时car为出队的元素的值。若出队成功(便道不为空)，则返回true，否则(便道空)，返回false，此时car不会返回有效值。
{
    if (!SideWalk_IsEmpty(sidewalk))
    {
        car = sidewalk->sidewalkCar[sidewalk->front];
        sidewalk->front = (sidewalk->front + 1) % sidewalk->max;
        return true;
    }
    else return false;
}

void SideWalk_Print(SideWalk* sidewalk)
// 依次打印出队列中的每个元素。
{
    int i = sidewalk->front;
    if (SideWalk_IsEmpty(sidewalk)) {
        printf("sidewalk is emtpy");
        return;
    }
    for (i = sidewalk->front; i != sidewalk->rear; i = (i + 1) % sidewalk->max) {
        printf("%s  ", sidewalk->sidewalkCar[i].license_plate);
    }
    printf("\n");
}





